﻿#include "GLLInc.h"
#include "GLLCamera.h"
#include "GLLVirtualWorld.h"
#include "GLLTheSystem.h"
#include "GLLLevel.h"

namespace GLL
{

	ObjectCamera::ObjectCamera()
	{
		_l = 0;
		_t = 0;
		_w = 1.0f;
		_h = 1.0f;
		_z = 0;
		_cam = 0;
		_canvas = 0;
	}

	void ObjectCamera::_Add2Level( Level* plvl)
	{
		_cam = plvl->_smOgre->createCamera(StrID());
		_cam->setNearClipDistance(0.01f);

		_canvas = theWorld()->_renderWindow->addViewport(_cam, _z, _l, _t, _w, _h);
		_canvas->setBackgroundColour(Ogre::ColourValue(127.5, 127.5, 127.5));

		AdjustAspectRatio();

		SetPosition(_pos);
		SetLookAt(_look);
	}

	void ObjectCamera::_RemoveFromLevel( Level* plvl)
	{
		if(_cam) plvl->_smOgre->destroyCamera(_cam);
	}

	void ObjectCamera::SetPosition( const Position3Df& v)
	{
		_pos = v;
		if(_cam){
			_cam->setPosition(v.x, v.y, v.z);
		}
	}

	Position3Df ObjectCamera::GetPosition()
	{
		return _pos;
	}

	void ObjectCamera::SetLookAt( const Position3Df& v)
	{
		_look = v;
		if(_cam){
			_cam->lookAt(v.x, v.y, v.z);
		}
	}

	void ObjectCamera::AdjustAspectRatio()
	{
		if(_cam) _cam->setAspectRatio(Ogre::Real(_canvas->getActualWidth()) / Ogre::Real(_canvas->getActualHeight()));
	}

	void ObjectCamera::SetCanvas( float left, float top, float width, float height)
	{
		_l = left;
		_t = top;
		_w = width;
		_h = height;
		if(_canvas) _canvas->setDimensions(_l, _t, _w, _h);
	}

	GLL::Axis3Df ObjectCamera::GetRotation()
	{
		return Axis3Df::ZERO();
	}
};//GLL
